// var username = prompt("输入您的用户名","");
var username = 'lzc';


// 储存信息卡片样式
let cardClass = [["onelend","onereturn","onereview"],["onebook","onebookadd"],["onemember","onehit"],["","onenotice"]];
let cardStyle = new Array();
for(let i = 0;i<4;i++) {
	cardStyle[i] = new Array();
}
cardStyle[0][0] = "<button type='button' class='reasonMore lend-refuse'>拒绝</button><button type='button' class='lend-pass'>同意</button><button type='button' class='cardMore lend-more'>详情</button>";
cardStyle[0][1] = "<button type='button' class='reasonMore return-refuse'>拒绝</button><button type='button' class='return-pass'>同意</button><button type='button' class='cardMore return-more'>详情</button>";
cardStyle[0][2] = "<button type='button' class='cardMore review-more'>详情</button>";
	
cardStyle[1][0] = "<button type='button' class='book-delete'>删除</button><button type='button' class='book-compile'>编辑</button><button type='button' class='cardMore book-more'>详情</button>";
cardStyle[1][1] = "<button type='button' class='bookadd-ignore'>忽略请求</button><button type='button' class='bookadd-pass'>添加入库</button><button type='button' class='cardMore bookadd-more'>详情</button>";

cardStyle[2][0] = "<button type='button' class='member-hit'>加入黑名单</button><button type='button' class='cardMore member-more'>详情</button>";
cardStyle[2][1] = "<button type='button' class='hit-delete'>移出黑名单</button><button type='button' class='cardMore hit-more'>详情</button>";

cardStyle[3][0] = "";
cardStyle[3][1] = "<button type='button' class='notice-delete'>删除</button>";




// 渲染信息函数
function addcardDate( activefather, activeson, response, targetbox) {
	// 储存传递信息样式
	let dateStyle = new Array();
	for(let i = 0;i<4;i++) {dateStyle[i] = new Array();}
	
	let cardcounts = response.data.length;
	
	for(let i = 0;i<cardcounts;i++) {
		
		
		if(activefather==0) {
			// if(activeson==0)dateStyle[0][0] =
			// if(activeson==1)dateStyle[0][1] = 
			if(activeson==2)dateStyle[0][2] = "<table><tr><th align='right'>姓名：</th><td>"
							+response.data[i].username+"</td></tr><tr><th align='right'>书名：</th><td>"
							+response.data[i].name+"</td></tr><tr><th align='right'>审批类型：</th><td>"
							+response.data[i].state+"</td></tr><tr><th align='right'>时间：</th><td>"
							+response.data[i].date2+"</td></tr></table>";
		}
		if(activefather==1) {
			// if(activeson==0)dateStyle[1][j] =
			// if(activeson==1)dateStyle[1][j] = 
		}
		if(activefather==2) {
			if(activeson==0)dateStyle[2][0] = "<table><tr><th align='right'>姓名：</th><td>"
									+response.data[i].name+"</td></tr><tr><th align='right'>性别：</th><td>"
									+response.data[i].gender+"</td></tr><tr><th align='right'>专业班级：</th><td>"
									+response.data[i].class+"</td></tr><tr><th align='right'>ID：</th><td>"
									+response.data[i].userName+"</td></tr></table>";
			if(activeson==1)dateStyle[2][1] = "<table><tr><th align='right'>姓名：</th><td>"
									+response.data[i].name+"</td></tr><tr><th align='right'>性别：</th><td>"
									+response.data[i].gender+"</td></tr><tr><th align='right'>专业班级：</th><td>"
									+response.data[i].class+"</td></tr></table>";
		}
		if(activefather==3) {
			if(activeson==0)dateStyle[3][0] = "";
			if(activeson==1)dateStyle[3][1] = "<table><tr><th align='right'>标题：</th><td>"
									+response.data[i].title+"</td></tr><tr><th align='right'>备注：</th><td>"
									+response.data[i].ex_infor+"</td></tr><tr><th align='right'>日期：</th><td>"
									+response.data[i].class+"</td></tr></table>";
		}
		
		
		
		if(targetbox.children.length>2) {
			for(let i = 0;i<cardcounts;i++) {
				let repetition = targetbox.children[i+2].nodeName;
				targetbox.removeChild(repetition);
			}
		}
	
		let itsid = response.data[i].id;
		// console.log(itsid);
		// 添加卡片+信息
		let thiscard = document.createElement("span");
		thiscard.classList.add("oneCard",cardClass[activefather][activeson]);
		thiscard.id = itsid;
		thiscard.innerHTML = dateStyle[activefather][activeson]+cardStyle[activefather][activeson];
		targetbox.appendChild(thiscard);
		
		
		popUp(response);
	}
}


// 定义各操作页面切换功能
var that;
let activemgt;
let activesoncard;
let mgtsons;
let inShowcards;
let onecardcounts;
class cardsTurn {
	constructor(id) {
	    that = this;
		this.main = document.querySelector(id);
		this.cards = this.main.querySelectorAll('.cards');
		this.mgtCards = document.querySelector('#management').children;
		this.backhome = document.querySelector('#backhome');
		this.admEsc = document.querySelector('#Esc');
		this.soncards = document.querySelectorAll('.son-cards');
		this.init();
	}
	init() {
		for(let i = 0;i<this.cards.length;i++) {
			this.cards[i].index = i;
			this.cards[i].onclick = this.turnCards;
			this.cards[i].ondblclick = this.backHome;
			that.mgtCards[i].style.display = 'none'; 
		}
		that.backhome.onclick = this.backHome;
		that.admEsc.onclick = this.admEsc;
		 
	}
		// 侧边栏
	// 功能卡片切换
	turnCards() {
		// console.log(this.index);
		activemgt = this.index;
		that.clearCards();
		this.className = 'cards cardactive';
		that.mgtCards[this.index].style.display = 'block';
		// 数据初始化
		let targetMgt = that.mgtCards[this.index].children[0];
		if(this.index==0) {
			console.log(targetMgt.id);
			
		}
		if(this.index==1) {
			console.log(targetMgt.id);
			
		}
		if(this.index==2) {
			console.log(targetMgt.id);
			axios.post('http://yuntenglibrary.natapp1.cc/Administrator/userMana/Com', {
				userName:username
			}).then(response => {
				// console.log(response.data);
				// addcardDate( activemgt, 0, response, targetBox);
				
			})
		}
		// 初始化子模块切换
		that.soninit(activemgt);
	}
	// 返回欢迎界面
	backHome() {
		that.clearCards();
	}
	// 退出（返回登录界面）
	admEsc() {
		
	}
	// 清除功能卡片
	clearCards() {
		for(let i = 0;i<this.cards.length;i++) {
			this.cards[i].className = 'cards';
			that.mgtCards[i].style.display = 'none';
		}
	}
		// 子模块切换
	// 子模块初始化
	soninit(activemgt) {
		activesoncard = 0;
		// console.log(activemgt);
		mgtsons = that.mgtCards[activemgt].querySelectorAll('.sonmgt').length-1;
		// console.log(mgtsons);
		that.turnsonMgt();
	}
	// 子模块切换
	turnsonMgt() {
		that.initSon();
		for(let j = 0;j<=mgtsons;j++) {
			that.soncards[activemgt].children[j].addEventListener("click",function(){
			    activesoncard = j;
				that.clearsonMgt();
				that.soncards[activemgt].children[activesoncard].className = 'sonactive';
				that.mgtCards[activemgt].children[activesoncard].style.display = '';
			});
		}
	}
	// 子模块初始化
	initSon() {
		that.clearsonMgt();
		that.soncards[activemgt].children[activesoncard].className = 'sonactive';
		that.mgtCards[activemgt].children[activesoncard].style.display = '';
		inShowcards = that.mgtCards[activemgt].children[activesoncard].querySelectorAll('.oneCard');
		onecardcounts = inShowcards.length;
		// console.log(onecardcounts);
		
		// 切换-数据初始化
		
		that.soncards[activemgt].addEventListener("click",function(e) {
			let event = e||window.event;
			let target = event.target||event.srcElement;
			
			if(target.innerHTML=="借阅申请") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				
			}
			if(target.innerHTML=="归还申请") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				
			}
			if(target.innerHTML=="审批记录") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				
				axios.get('http://fu6uvf.natappfree.cc/YUNTENG_war/main', {
			    }).then(response => {
					addcardDate( activemgt, activesoncard, response, targetBox);
				})
			}
			if(target.innerHTML=="更改") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				
				
			}
			if(target.innerHTML=="添加") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				
				
			}
			if(target.innerHTML=="全部用户") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				axios.post('http://yuntenglibrary.natapp1.cc/Administrator/userMana/Com', {
					userName:username
				}).then(response => {
					console.log(response.data);
					addcardDate( activemgt, activesoncard, response, targetBox);
					
				})
			}
			if(target.innerHTML=="黑名单") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				
				axios.post('http://yuntenglibrary.natapp1.cc/Administrator/userMana/Bla', {
					userName:username
				}).then(response => {
					addcardDate( activemgt, activesoncard, response, targetBox);
					// console.log(response.data);
					// let cardcounts = response.data.length;
					
					// for(let i = 0;i<cardcounts;i++) {
					// 	let itsid = response.data[i].id;
					// 	// console.log(itsid);
					// 	addmsgCard(activemgt,activesoncard,targetBox,itsid);
					// 	let datetable = document.createElement("table");
					// 	let datefor = targetBox.getElementById(itsid);
					// 	datetable.innerHTML = ;
					// 	datefor.insertBefore(datetable,datefor.children[0]);
					// }
				})
			}
			if(target.innerHTML=="全部公告") {
				let targetBox = target.parentNode.parentNode.children[activesoncard];
				console.log(targetBox.id);
				console.log(target.innerHTML);
				axios.get('http://xf37rb.natappfree.cc/mysql/getboard').then(response => {
					console.log(response.data);
					addcardDate( activemgt, activesoncard, response, targetBox);
					
				})
			}
		})
	}
	// 子模块清除
	clearsonMgt() {
		for(let k = 0;k<=mgtsons;k++) {
			this.soncards[activemgt].children[k].className = '';
			that.mgtCards[activemgt].children[k].style.display = 'none';
		}
	}
	
}
		
new cardsTurn('#cardsmenu');	// 页面操作功能切换启动



// 定义详情弹窗功能

function popUp(response) {
	document.querySelector('#management').addEventListener("click", function(e) {
		let event = e||window.event;
		let target = event.target||event.srcElement;
		let onclickbutton = target.parentNode;
		// 传递点击卡片的id
		let needid = onclickbutton.id;
		let cardcounts = response.data.length;
		for(let i = 0; i<cardcounts;i++) {
			let itsid = response.data[i].id;
			if(itsid==needid)break;
		}
		// 详情弹窗
		if(onclickbutton.querySelector('.cardMore') == target) {
			onclickbutton.parentNode.querySelector('.allmore').style.display = 'block';
			onclickbutton.parentNode.querySelector('.allmore').querySelector('tbody').style.display = '';
			onclickbutton.parentNode.querySelector('tfoot').style.display = 'none';	
			
		}
		// 拒绝弹窗
		if(onclickbutton.querySelector('.reasonMore') == target) {
			onclickbutton.parentNode.querySelector('.allmore').style.display = 'block';
			onclickbutton.parentNode.querySelector('tfoot').style.display = '';
			onclickbutton.parentNode.querySelector('.allmore').querySelector('tbody').style.display = 'none';
		}
		// 弹窗切换
		if(onclickbutton.querySelector('.reasonSure') == target) {
			onclickbutton.parentNode.parentNode.nextElementSibling.style.display = '';
			onclickbutton.parentNode.parentNode.style.display = 'none';
		}
		
		// 关闭弹窗
		let moreBack = 	target.parentNode;
		// console.log(moreBack.innerHTML);
		if(moreBack.querySelector('.moreback') == target) {
			moreBack.parentNode.style.display = '';
		}		
	});				
}		



// 发布公告功能
function newnoticeSub() {
	
}